import java.util.LinkedList;
import java.util.Queue;
//牛客225. 用队列实现栈
class MyStack {
    Queue<Integer> s1;
    Queue<Integer> s2;

    public MyStack() {
        s1 = new LinkedList<>();
        s2 = new LinkedList<>();

    }
    
    public void push(int x) {
        if(!s2.isEmpty()){
            s2.offer(x);
        }else{
            s1.offer(x);
        }
    }
    
    public int pop() {
        if(empty()){
            return -1;
        }else if(!s1.isEmpty()){
            int size = s1.size();
            for(int i = 0; i < size - 1; i++){
                s2.offer(s1.poll());
            }
            return s1.poll();
        }else{
            int size = s2.size();
            for(int i = 0; i < size - 1; i++){
                s1.offer(s2.poll());
            }
            return s2.poll();
        }
    }
    
    public int top() {
        int ret = -1;
        if(empty()){
            return -1;
        }else if(!s1.isEmpty()){
            int size = s1.size();
            for(int i = 0; i < size; i++){
                ret = s1.poll();
                s2.offer(ret);
            }
            return ret;
        }else {
            int size = s2.size();
            for(int i = 0; i < size; i++){
                ret = s2.poll();
                s1.offer(ret);
            }
            return ret;
        }
      
    }
    
    public boolean empty() {
        return s1.isEmpty() && s2.isEmpty();
    }
}